<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['send_status']=plugin('jy_weishop')->send_status;
$return['status']=$return['status2']=plugin('jy_weishop')->order_status;
$return['buytype']=plugin('jy_weishop')->buy_types;
$return['sendtype']=plugin('jy_weishop')->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
$install = check_account_permission('jy_peisong');
if($install){
	model('jy_peisong/order')->order_checktime();
}
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	$params=array('merchid'=>0,'sendtype'=>1,'uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
    $orderby='id desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['status'])){
		if($_RQ['status']==15){
			$params['status']=array(11,12,13,14,15,16);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$tip='所有订单显示，列表商品只显示第一个！';
	$tip .='</br><red>【注意】：本场景已开启独立商品展示，只显示本场景下的订单！</red>';
	$tip .='</br>【<red>订单时间显示不准确？</red>】：可能服务器的时间不准确，联系管理员同步一下服务器网络时间！';
	$onepluginname=model('jy_weishop/goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$params['plugin_name']=$onepluginname;
			$return['params']['plugin_name']=$onepluginname;
	}else{
			//$params['plugin_name']='';
	}
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['member']['nickname'] .=' (ID:'.$item['mid'].')';
		$goods=pdo_get('jy_weishop_order_goods',array('orderid'=>$item['id']),array('goodsid','total'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid']),array('title','image','unit'));
		$item['total']=$goods['total'];
		$item['title']=$goodsinfo['title'];
		$item['image']=$goodsinfo['image'];
		$item['unit']=$goodsinfo['unit'];
		if(!$item['merchid'] && $item['storeid']){
			$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title');
		}
		$item['storetitle']=$item['storetitle']?:'平台自营';
		$item['expressid']=pdo_getcolumn('jy_weishop_order_express',array('orderid'=>$item['id']),'id');
		$address=$item['address']?json_decode($item['address'],true):$item['addressid'];
		if($address){
			$item['addressinfo']=model('core/member')->get_address_str($address);
		}
		$item['realname']=$address['realname']?:$address['name'];
		$item['noexpressid']=$item['expressid']?0:1;
		$item['nostatus1']=$item['status']==1?0:1;
		$fahuo_log=pdo_get('jy_fahuo_log',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']));
		$item['fahuo_logid']=$fahuo_log['id'];
		if($fahuo_log['printdata']){
			$item['noprint2']=0;
		}else{
			$item['noprint2']=1;
		}
		if($item['status']==1){
			$item['fahuo_logstatus']=$fahuo_log['status'];
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理_Order list'),
		),
		'list'=>array('data'=>'list','openvoice'=>true,'openvoice_action'=>'jy_weishop/order.list/getnew/','tip'=>$tip,'edit'=>false,'del_one'=>false,'delall'=>false,'total'=>$total,'psize'=>$psize,'html_to'=>'list_html','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('type'=>'string_more','title'=>'支付/状态_Is pay',
			'data'=>array(
				array('title'=>'是否支付_Is pay?','name'=>'ispay','type'=>'radio','options'=>'ispay'),
				array('title'=>'支付方式_Pay type','name'=>'paytype','type'=>'radio','options'=>'paytype')
				)
			),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'realname','title'=>'客户名称','type'=>'string'),
			array('name'=>'addressinfo','title'=>'发货地址','type'=>'string'),
			array('type'=>'post_more','title'=>'快递信息',
				'data'=>array(
					array('action'=>'jy_weishop/order.express/','name'=>'expressinfo','type'=>'string','noshow'=>'noexpressid'),
					array('op'=>'print','title'=>'获取面单并打印发货','type'=>'href','noshow'=>'nostatus1'),
					array('op'=>'editfa','title'=>'手动添加','type'=>'modal','noshow'=>'nostatus1'),
					array('op'=>'editfa','title'=>'修改','type'=>'modal','noshow'=>'noexpressid'),
					array('op'=>'print','title'=>'重新打印','type'=>'href','noshow'=>'noprint2'),
				)
			),
		),
		'action'=>array(
			array('action'=>'jy_weishop/order.express/','title'=>'查看物流','type'=>'href','show'=>array('expressid >'=>0)),
			array('type'=>'href','action'=>'jy_weishop/order.detail/','title'=>'订单详情'),
			array('type'=>'post','op'=>'quxiaomian','title'=>'取消面单','show'=>array('fahuo_log >'=>0,'fahuo_logstatus'=>0)),
			array('type'=>'post','op'=>'sendmian','title'=>'通知取件','show'=>array('fahuo_log >'=>0,'fahuo_logstatus'=>0)),
		),'batch'=>array(
			//array('type'=>'href','url'=>'/order/setfa/','title'=>'一键打印快递单'),
		)),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'订单状态_Status','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>10,'title'=>'订单类型_Type','type'=>'radio','options'=>'buytype'),
			array('name'=>'merchid','width'=>10,'title'=>'所属商户_Merch','type'=>'select','option_value'=>'id','option_title'=>'merchname','options'=>'merchs'),
			array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'core/member.get_member/'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
	}else{
		$merchs=array();
	}
	$merchs[0]=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	foreach($merchs as &$mv){
		$mv['title']=$mv['merchname'];
	}
	$return['merchs']=$merchs;
	set_html('title','发货单管理');
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$member=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','mobile'));
		$v['nickname']=$member['nickname'];
		$v['usermobile']=$member['mobile'];	
		if($item['address']){
			$address=json_decode($item['address']);
		}else{
			$address=pdo_get('core_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		}
		$v['address']=model('core/member')->get_address_str($v['addressid']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['paytime']=date('Y-m-d H:i:s',$v['paytime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid'));
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$selespename='无规格';
			}
			$num=$k+1;
			$v['goodstitle'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
		}
		$v['diyformdata']=json_decode($v['diyformdata'],true);
		$v['data2']='';
		foreach($v['diyformdata'] as $dk=>&$diyv){
				$v['data2'] .='【'.$diyv['title']."】：";
				if(is_array($diyv['data']) && $diyv['type']!=7){
					$diyv['data']=$diyv['data'][0].' '.$diyv['data'][1].' '.$diyv['data'][2].' '.$diyv['data'][3];
				}
				if($diyv['type']==7){
					$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
				}
				$v['data2'] .=$diyv['data'];
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'storetitle'=>'所属门店',
		'goodstitle'=>'商品名称',
		'nickname'=>'买家昵称',
		'usermobile'=>'买家电话',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'paytime'=>'支付时间',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
		'remark'=>'备注',
		'data2'=>'表单内容',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='setfa'){
	$basic=$this->plugin_setting('basic');
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['express']){
			exi('请选择快递公司！','error');
		}
		$ids=$post['ids'];
		$nowkuai=$basic['kuaidis'][$post['kuaikey']];
		$cpCode=$nowkuai['name']?:'SF';
		if(!$cpCode){
			exi('请选择快递公司！','error');
		}
		$express_id=$nowkuai['express_id'];
		$templateUrl=$this->model('model')->get_templateUrl($cpCode);
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				$order=pdo_get('jy_weishop_order',array('id'=>$id));
				$order['callDoorPickUp']=$post['isyue']?true:false;
				$order['doorPickUpTime']=$post['starttime'];
				$order['doorPickUpEndTime']=$post['endtime'];
				$res=$this->model('api')->get_waybill($order,$cpCode,$templateUrl,$nowkuai);
				//print_r($res);exit;
				$cpCode=$res[0]['cpCode'];
				$expresssn=$res[0]['waybillCode'];
				if(!$expresssn){
					exi('获取面单发货失败！','error');
				}
				$data=array('express'=>$express_id,'expresssn'=>$expresssn);
				model('jy_weishop/order')->order_send($id,$data);
				$data['cpcode']=$cpCode;
				$data['templateurl']=$templateUrl;
				$data['starttime']=$post['starttime'];
				$data['endtime']=$post['endtime'];
				$this->model('model')->uplog($order,$data);
			}
		}
		exi('一键发货成功！');
	}
	$ids=trim($_RQ['ids'],';');
	if($ids){
			$ids=explode(';',$ids);
	}else{
			$ids=array();
	}
	if($_RQ['id']){
			$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
			exi('请先选择订单','error');
	}
	$item=array('ids'=>$ids);
	$htmls=array(
			'type'=>'edit',
			'desc'=>'打印快递单',
	);
	$htmls['edit']=array(
			array('type'=>'select','title'=>'快递公司','name'=>'kuaikey','options'=>'express'),
			array('type'=>'switch','title'=>'是否预约上门','name'=>'isyue'),
			array('type'=>'time','title'=>'预约上门收件时间','name'=>'starttime','show'=>array('isyue'=>1)),
			array('type'=>'time','title'=>'预约上门截止时间','name'=>'endtime','show'=>array('isyue'=>1)),
	);
	$return['express']=$basic['kuaidis'];
	foreach($return['express'] as $k=>&$ev){
		$ev['id']=$k;
	}
	//$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='print'){
	$basic=$this->plugin_setting('basic');
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($post['fahuo_logid']){
			$data=pdo_get('jy_fahuo_log',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
		}else{
			$nowkuai=$basic['kuaidis'][$post['kuaikey']];
			$cpCode=$nowkuai['name']?:'SF';
			if(!$cpCode){
				exi('请选择快递公司！','error');
			}
			$templateUrl=$this->model('model')->get_templateUrl($cpCode);
			//print_r($templateUrl);exit;
			$order['callDoorPickUp']=$post['isyue']?true:false;
			$order['doorPickUpTime']=$post['starttime'];
			$order['doorPickUpEndTime']=$post['endtime'];
			$res=$this->model('api')->get_waybill($order,$cpCode,$templateUrl,$nowkuai);
			//file_put_contents(ROOT_D.'/1.txt','~~取号接果'.ijson_encode($res)."\n",FILE_APPEND);
			//print_r($res);exit;
			$cpCode=$res[0]['cpCode'];
			$express_id=$nowkuai['express_id'];
			$expresssn=$res[0]['waybillCode'];
			if(!$expresssn){
				exi('获取面单发货失败！','error');
			}
			$data=array('express'=>$express_id,'expresssn'=>$expresssn);
			model('jy_weishop/order')->order_send($id,$data);
			$data['cpcode']=$cpCode;
			$data['templateurl']=$templateUrl;
			$data['starttime']=$post['starttime'];
			$data['endtime']=$post['endtime'];
			$data['printdata']=$res[0]['printData'];
			$data['id']=$this->model('model')->uplog($order,$data);
		}
		$data['preview']=$post['preview'];
		$return['printmessage']=$this->model('model')->upprintmessage($order,$data);
		exi($return);
	}
	$id=intval($_RQ['id']);
	$item=array('id'=>$id);
	$fahuo_log=pdo_get('jy_fahuo_log',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
	$item['fahuo_logid']=$fahuo_log['id'];
	$htmls=array(
			'type'=>'edit',
			'desc'=>'打印快递单',
	);
	$htmls['edit']=array(
			array('type'=>'select','title'=>'快递公司','name'=>'kuaikey','options'=>'express'),
			array('type'=>'switch','title'=>'是否预约上门','name'=>'isyue'),
			array('type'=>'time','title'=>'预约上门收件时间','name'=>'starttime','show'=>array('isyue'=>1)),
			array('type'=>'time','title'=>'预约上门截止时间','name'=>'endtime','show'=>array('isyue'=>1)),
	);
	$return['express']=$basic['kuaidis'];
	foreach($return['express'] as $k=>&$ev){
		$ev['id']=$k;
	}
	//$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='editfa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['express']){
			exi('请选择快递公司！','error');
		}
		if(!$post['expresssn']){
			exi('请填写快递单号！','error');
		}
		$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
		model('jy_weishop/order')->order_send($id,$data);
		exi('修改快递信息成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=array('id'=>$id);
	$htmls=array(
			'type'=>'edit',
			'desc'=>'快递信息',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name'),
		array('type'=>'string','title'=>'快递单号','name'=>'expresssn'),
	);
	$return['express']=$this->express_types;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='quxiaomian'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$express=pdo_get('jy_weishop_order_express',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$log=pdo_get('jy_fahuo_log',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid'],'status'=>0));
	if(!$log['expresssn'] || !$log['cpcode']){
		exi('不存在公司编号，快递单号！','error');
	}
	$res=$this->model('api')->del_waybill($log['cpcode'],$log['expresssn']);
	pdo_update('jy_fahuo_log',array('status'=>4),array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('取消面单成功！','','list');
}
?>